package cn.wolfcode.web.controller;

import cn.wolfcode.domain.Permission;
import cn.wolfcode.qo.JsonResult;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IPermissionService;
import cn.wolfcode.util.RequiredPermission;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/permission")
public class PermissionController {
    private IPermissionService permissionService;

    @Autowired
    public void setPermissionService(IPermissionService permissionService) {
        this.permissionService = permissionService;
    }

    //处理查询所有权限的请求,响应HTML
    //permission/*.do
    //@RequiredPermission(name = "权限页面", expression = "permission:list")
    @RequestMapping("/list")
    public String list(Model model, QueryObject qo) {
        //分页
        IPage<Permission> query = permissionService.query(qo);
        model.addAttribute("pageInfo", query);
        return "permission/list";// 找视图 /WEB-INF/views/permission/list.jsp
    }


    //@RequiredPermission(name = "权限加载", expression = "permission:reload")
    @RequestMapping("/reload")
    @ResponseBody
    public JsonResult reload() {
        //重新加载权限
//        try {
        permissionService.reload();
        JsonResult result = new JsonResult();
        System.out.println(result);
        return result;
//        } catch (Exception e) {
//            e.printStackTrace();
//            return new JsonResult(false,"权限加载失败");
//        }

    }
}

